//index.js
//获取应用实例
const app = getApp();

Page({
  data: {
    sex_array: ['保密','男','女'],
    userInfo: {},
    hasUserInfo: false,
    // canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },
  //事件处理函数
 
  onLoad: function () {
    let userInfo=getApp().globalData.userInfo;
    if (userInfo) {
      if(userInfo.gender==0){
        userInfo.gender= this.data.sex_array[0]
      }else if(userInfo.gender==1){
        userInfo.gender= this.data.sex_array[1]
      }else{
        userInfo.gender= this.data.sex_array[2]
      }
      this.setData({
        userInfo: userInfo,
        hasUserInfo: true
      })
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      // app.userInfoReadyCallback = res => {}
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      this.setData({
        userInfo: {},
        hasUserInfo: false
      })
    }
  },
  getUserInfo: function(e) {
    console.log("e:",e)
    let curObj= e.detail.userInfo;
    let userInfo = {
      avatarUrl: curObj.avatarUrl,
      country: curObj.country,
      gender: curObj.gender,
      nickName: curObj.nickName,
      city: curObj.city,
      language: curObj.language,
      province: curObj.province,
      // __proto__: curObj.__proto__
    };
    if(userInfo.gender==0){
      userInfo.gender= this.data.sex_array[0]
    }else if(userInfo.gender==1){
      userInfo.gender= this.data.sex_array[1]
    }else{
      userInfo.gender= this.data.sex_array[2]
    }
    app.globalData.userInfo = userInfo;
    this.setData({
      userInfo:userInfo,
      hasUserInfo: true
    })
    wx.showToast({
      title: '登录成功',
      icon:'success'
    })
  },
  exitLogin() {
    this.setData({
      userInfo:{},
      hasUserInfo: false,
    })
    wx.showToast({
      title: '已退出',
      icon:'none'
    })
  },
  onShareAppMessage: function (res){
    return {
      title: '电影之家',
      path: '/pages/index/index'
    }
  },
})
